/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.atp.agencia.ui;

import br.com.atp.agencia.principal.JPrincipal;
import br.com.atp.agencia.controle.Pousada;
import br.com.atp.agencia.modelo.Aluguel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;

/**
 *
 * @author Afonso
 */
public class JIlFEscolheAcomodacao extends JInternalFrame implements ActionListener {

    private Pousada p1;
    private JPanel panel;
    int h = 700;
    int w = 250;
    

    public JIlFEscolheAcomodacao(Pousada p1) {
        super();
        this.p1 = p1;
        initComponents();
    }

    private void initComponents() {
        setTitle("Acomodacoes");
        setLayout(new BorderLayout());
        panel = new JPanel();
        panel.setLayout(new FlowLayout());
        add(panel, BorderLayout.CENTER);
        setSize(h, w);
        setVisible(true);
        preencheInternalFrame();
    }

    private void preencheInternalFrame() {
        for (Aluguel a1 : p1.getAlugueis()) {
            String text = String.valueOf(p1.getPosicao(a1) + 1);
            //text += " \n"+ a1.getClass().getSimpleName();
            JButton button = new JButton(text);
            if (a1.isOcupado()) {
                button.setBackground(Color.red);
            } else {
                button.setBackground(Color.GREEN);
            }
            button.setSize(h + 90, w + 90);
            button.addActionListener(this);
            addToltip(button, a1);
            panel.add(button);
            panel.revalidate();
            validate();
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int op = Integer.parseInt(e.getActionCommand()) - 1;
        System.out.println("Escolheu a acomodação: " + e.getActionCommand());
        JPrincipal.aluguelTrabalhado = p1.getAluguel(op);
        dispose();
    }

    private void addToltip(JButton button, Aluguel a1) {
        if (a1.isOcupado()) {
            button.setToolTipText(a1.getClass().getSimpleName() + " " + "Ocupado");
        } else {
            button.setToolTipText(a1.getClass().getSimpleName() + " " + "Livre");
        }
    }
}
